package com.holystone.splunk.app.common;

import com.holystone.splunk.app.service.IIndexAlertService;
import com.holystone.splunk.app.service.IIndexEventService;
import com.holystone.splunk.common.SystemConstant;
import com.holystone.splunk.app.service.ISplunkIndexService;
import com.splunk.Index;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * Copyright (c) 2017, HOLYSTONE Technologies, Inc.
 * All right reserved.
 *
 * @author huyan
 * @date 2017/11/7
 * @desc: 在系统启动时初始化index
 */
public class InitEnvironment {

    private Logger logger = Logger.getLogger(this.getClass());
    @Resource(name = "splunkIndexService")
    private ISplunkIndexService indexService;
    @Resource(name = "indexAlertService")
    private IIndexAlertService indexAlertService;
    @Resource(name = "indexEventService")
    private IIndexEventService indexEventService;

    /**
     * 初始化告警，查看是否有index=event_alert_index
     * 如果没有，就生成一个index
     */
    public void init() {
        logger.debug(">>>>> 正在初始化告警数据，请稍等......");
        try {
            //1
            Index index = indexService.getIndexByName(SystemConstant.SPLUNK_INDEX);
            if (index == null) {
                indexService.createNewIndex(SystemConstant.SPLUNK_INDEX);
            }
            //2 搭载splumk 数据
            //告警名称
            indexAlertService.getExistsAlertNames();
            //事件排序
            indexEventService.getExistsEventSorts();
        } catch (Exception e) {
            logger.error("初始化告警数据失败：" + e.getMessage(), e);
            e.printStackTrace();
        }

        logger.debug("<<<<<< 初始化告警数据完成");
    }
}
